﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Castle.DynamicProxy.Generators.Emitters.SimpleAST;
using DMS.Common.Model.Result;
using Microsoft.AspNetCore.Builder;
using SqlSugar.Template.Extensions;
using SqlSugar.Template.IService;
using SqlSugar.Template.IService.Param;
using SqlSugar.Template.IService.Result;
using SqlSugar.Template.Models;

namespace SqlSugar.Template.Service
{
    public class UseService : BaseService<Use>, IUseService
    {
        public async Task<ResponseResult> Add(AddUseParam param)
        {
            ResponseResult result = new();
            if (param == null
                ||string.IsNullOrWhiteSpace( param.Account)|| string.IsNullOrWhiteSpace(param.PassWord)
                )
            {
                result.errno = 1;
                result.errmsg = "参数错误";
                return result;
            }
            Use use = new()
            {
              Account=param.Account,
              PassWord= CryptographyHelper.GetMD5Hash( param.PassWord),
              Type=param.Type,
            };

            //插入返回自增列
            var flag = await base.InsertReturnIdentityAsync(use);
            //null 列不插入
            flag = await base.AsInsertable(use).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
            result.data = flag;
            return result;
        }

        public Task<ResponseResult> AddTran(AddBookParam param)
        {
            throw new NotImplementedException();
        }

        public async Task<ResponseResult> DeleteAsync(long Id)
        {
            ResponseResult result = new();
            var t1 = await Context.Deleteable<Use>()
                .Where(a => a.Id == Id)
                .ExecuteCommandAsync();
            result.data = t1;
            return result;

        }

        public Task<ResponseResult<UseResult>> GetBookAsync(string BookId)
        {
            throw new NotImplementedException();
        }

        public async Task<ResponseResult<UseResult>> GetUseByAccountAndPwdAsync(string Account, string Pwd)
        {
            ResponseResult<UseResult> result = new() { data = new UseResult() };
            var entity = await Context.Queryable<Use>()
                .Select<UseResult>()
                .FirstAsync(q => q.Account == Account&&q.PassWord== Pwd);
            if (entity == null)
            {
                result.errno = 1;
                result.errmsg = "未找到相关数据";
                return result;
            }
            result.data = entity;
            return result;

        }
      
        public async Task<ResponseResult<PageModel<UseResult>>> SearchUseAsync(SearchUseParam param)
        {
            ResponseResult<PageModel<UseResult>> result = new()
            {
                data = new PageModel<UseResult>()
            };
            RefAsync<int> totalCount = 0;
            var expression = Expressionable.Create<Use>();
            expression.And(m => m.Account.Contains(param.Account));
            Expression<Func<Use, bool>> where = expression.ToExpression();
            result.data = await base.QueryPageList<UseResult>(where, param, "id desc");
            return result;
        }

        public async Task<ResponseResult> UpdateAsync(EditUseParam param)
        {
            ResponseResult result = new();
            if (param.Id <= 0)
            {
                result.errno = 1;
                result.errmsg = "参数不合法";
                return result;
            }
            var t1 = await Context.Updateable(
                new Use()
                {
                    ModifyTime = DateTime.Now,
                    PassWord= CryptographyHelper.GetMD5Hash(param.PassWord)
                })
                .Where(a => a.Id == param.Id)
                .ExecuteCommandAsync();
            result.data = t1;
            return result;

        }
    }
}
